package com.anxin.registerManagement.controller;

import com.anxin.registerManagement.bean.jo.ProfessionSearchJo;
import com.anxin.registerManagement.bean.persist.Profession;
import com.anxin.registerManagement.service.ProfessionService;
import com.anxin.registerManagement.util.Assert;
import com.anxin.registerManagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "专业接口")
@RestController
@RequestMapping("/professions")
public class ProfessionController {

    private static final Logger logger = LoggerFactory.getLogger(ProfessionController.class);

    @Autowired
    ProfessionService professionService;

    @PostMapping("/search")
    public ResultUtil getProfessionList(@RequestBody ProfessionSearchJo professionSearchJo) {
        return ResultUtil.success(professionService.getProfessionList(professionSearchJo));
    }

    @ApiOperation("添加专业")
    @PostMapping("/add")
    public ResultUtil addProfession(@Valid @RequestBody Profession profession) {
        logger.info("添加专业：{}", profession);
        professionService.addProfession(profession);
        return ResultUtil.success();
    }

    @ApiOperation("修改专业")
    @PostMapping("/update")
    public ResultUtil updateProfession(@Valid @RequestBody Profession profession) {
        logger.info("修改专业：{}", profession);
        Assert.notNull(profession.getId(), "专业id不能为空");
        professionService.updateProfession(profession);
        return ResultUtil.success();
    }

    @ApiOperation("删除专业")
    @DeleteMapping("/delete")
    public ResultUtil deleteProfession(@RequestParam("id") int id) {
        logger.info("专业id：{}", id);
        Assert.notNull(id, "id不能为空!");
        professionService.deleteProfession(id);
        return ResultUtil.success();
    }

    @ApiOperation("获取所有专业")
    @GetMapping("/get")
    public ResultUtil getAllProfession() {
        return ResultUtil.success(professionService.getAllProfession());
    }
}
